package com.puhui.education.fragment.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.puhui.education.R;
import com.puhui.education.adapter.ViewHolder;
import com.puhui.education.adapter.base.recyclerview.CommonAdapter;
import com.puhui.education.bean.CourseMoreInfo;
import com.puhui.education.fragment.BaseFrag;
import com.puhui.education.util.DynamicLayoutParamsUtil;
import com.puhui.education.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/5/28.
 */
public class CourseFrag extends BaseFrag {
    @Bind(R.id.rv_displayCourseMoreInfo_courseFrag)
    RecyclerView rv_displayCourseMoreInfo;
    private Activity activity;
    private View view;
    private List<CourseMoreInfo> courseMoreInfoList;
    private List<FrameLayout> frameLayoutList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.frag_course, null);
        }
        ButterKnife.bind(this, view);
        initData();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }

    @Override
    protected void initData() {
        super.initData();
        frameLayoutList = new ArrayList<>();
        initCourseMoreInfo();
        rv_displayCourseMoreInfo.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        rv_displayCourseMoreInfo.setAdapter(new CommonAdapter<CourseMoreInfo>(activity, R.layout.item_rv_course_detail_coursefrag, courseMoreInfoList) {

            @Override
            public void convert(ViewHolder holder, final CourseMoreInfo courseMoreInfo) {
                FrameLayout frameLayout = holder.getView(R.id.fl_itemCourseFrag);
                if(cacheItemLayout(frameLayout)){
                    frameLayout.setLayoutParams(DynamicLayoutParamsUtil.createViewGroupLayoutParams(DynamicLayoutParamsUtil.WIDTH_MATCH, -1f, 0.22f));
                }
                holder.setText(R.id.tv_price_itemCourseFrag,"¥"+courseMoreInfo.getPrice());
                holder.setOnClickListener(R.id.fl_itemCourseFrag, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtil.showBottomToast("价格：¥"+courseMoreInfo.getPrice());
                    }
                });
            }
        });
    }

    private void initCourseMoreInfo() {
        courseMoreInfoList = new ArrayList<>();
        CourseMoreInfo info1 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 1f, 200,1);
        CourseMoreInfo info2 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 2f, 200, 2);
        CourseMoreInfo info3 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 3f, 200, 3);
        CourseMoreInfo info4 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 4f, 200, 4);
        CourseMoreInfo info5 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 5f, 200, 5);
        CourseMoreInfo info6 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 6f, 200, 6);
        CourseMoreInfo info7 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 7f, 200, 7);
        CourseMoreInfo info8 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 8f, 200, 8);
        CourseMoreInfo info9 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 9f, 200, 9);
        CourseMoreInfo info10 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 10f, 200, 10);
        CourseMoreInfo info11 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 11f, 200,1);
        CourseMoreInfo info12 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 12f, 200, 2);
        CourseMoreInfo info13 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 13f, 200, 3);
        CourseMoreInfo info14 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 14f, 200, 4);
        CourseMoreInfo info15 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 15f, 200, 5);
        CourseMoreInfo info16 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 16f, 200, 6);
        CourseMoreInfo info17 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 17f, 200, 7);
        CourseMoreInfo info18 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 18f, 200, 8);
        CourseMoreInfo info19 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 19f, 200, 9);
        CourseMoreInfo info20 = new CourseMoreInfo(R.mipmap.course, "初一数学寒假课程(北师版)", "本课程由中学数理化学科研究组专门", 20f, 200, 10);
        courseMoreInfoList.add(info1);
        courseMoreInfoList.add(info2);
        courseMoreInfoList.add(info3);
        courseMoreInfoList.add(info4);
        courseMoreInfoList.add(info5);
        courseMoreInfoList.add(info6);
        courseMoreInfoList.add(info7);
        courseMoreInfoList.add(info8);
        courseMoreInfoList.add(info9);
        courseMoreInfoList.add(info10);
        courseMoreInfoList.add(info11);
        courseMoreInfoList.add(info12);
        courseMoreInfoList.add(info13);
        courseMoreInfoList.add(info14);
        courseMoreInfoList.add(info15);
        courseMoreInfoList.add(info16);
        courseMoreInfoList.add(info17);
        courseMoreInfoList.add(info18);
        courseMoreInfoList.add(info19);
        courseMoreInfoList.add(info20);
    }
    //用于缓存布局,减少布局LayoutParams的设置
    private boolean cacheItemLayout(FrameLayout frameLayout) {
        boolean isAddEnable = true;
        if (frameLayoutList.size() != 0) {
            for (FrameLayout s : frameLayoutList) {
                if (frameLayout == s) {
                    isAddEnable = false;
                }
            }
        } else {
            frameLayoutList.add(frameLayout);
            return isAddEnable;
        }
        if (isAddEnable) {
            frameLayoutList.add(frameLayout);
        }
        return isAddEnable;
    }

    public void getData() {
        ToastUtil.showBottomToast("响应");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
